﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;


namespace TestCutOutImage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            try
            {
                //pictureBox1.Image = ClipBitmap("01.png", new Rectangle(700, 1200, 1100, 1100));
                Bitmap bmp = ClipBitmap("01.png", new Rectangle(750, 1310, 1000, 1000));
                pictureBox1.Image = bmp;
               // pictureBox1.Image = Bitmap.FromFile("02.png");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        /// <summary>
        /// 截取区域
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <param name="r">截取区域像素</param>
        /// <returns>截取到的图像</returns>
        public Bitmap ClipBitmap(string filename, Rectangle r)
        {
            Image img = Bitmap.FromFile(filename) as Image;
            Bitmap bmp = img as Bitmap;
            Bitmap bitmap = new Bitmap(r.Width + 1, r.Height + 1);
            for (int x = r.X; x <= (r.Width+r.X); x++)
            {
                for (int y = r.Y; y <= (r.Y+r.Height); y++)
                {
                    //取当前像素点
                    Color pixeColor = bmp.GetPixel(x, y);
                    byte R = pixeColor.R;
                    byte G = pixeColor.G;
                    byte B = pixeColor.B;
                    bitmap.SetPixel(x-r.X,y-r.Y,Color.FromArgb(R,G,B));
                    //给新的值
                    //if (IsPixel(bmp, x, y))//黑色
                    //{
                    //    bitmap.SetPixel(x - r.X, y - r.Y, Color.Black);
                    //}
                }
            }
            return bitmap;
        }

        /// <summary>
        /// 判断当前位置是否为需要的颜色
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public bool IsPixel(Bitmap bmp, int x, int y)
        {
            Color pixeColor = bmp.GetPixel(x, y);
            byte R = pixeColor.R;
            byte G = pixeColor.G;
            byte B = pixeColor.B;
            int r = Convert.ToInt32(R);
            if (Convert.ToInt32(G) == 170)
            {
                return true;
            }
            if (r == 0)//黑色
            {
                return true;
            }
            return false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(DecodeQrCode(pictureBox1.Image as Bitmap));
        }

        /// <summary>
        /// 解密二维码
        /// </summary>
        /// <param name="barcodeBitmap">图片</param>
        /// <returns>二维码信息</returns>
        private string DecodeQrCode(Bitmap barcodeBitmap)
        {
            //BarcodeReader reader = new BarcodeReader();
            //reader.Options.CharacterSet = "UTF-8";//GB2312  UTF-8  ASCII  Unicode
            //var result = reader.Decode(barcodeBitmap);
            //return (result == null) ? null : result.Text;
            MultiFormatReader mutiReader = new MultiFormatReader();
            Bitmap img = barcodeBitmap;
            if (img == null)
                return "";
            LuminanceSource ls = new RGBLuminanceSource(Encoding.UTF8.GetBytes(img.ToString()), img.Width, img.Height);
            BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(ls));

            Result result = mutiReader.decode(bb);
            //解码设置编码方式为：utf-8，

            
            //String resultStr = result.getText(); 
            return (result == null) ? null : result.Text;
        }


    }
}
